<?php
	$_nav='';
	$_title='首页';
	include_once 'head.php';
	
	$infoc=new info();
	$userc=new user();

	/******评论start*******/
	if($_GET['action']=='comment'){
		$table='admin_topic_comment';
		$id=$_POST['id'];			  //小编说ID
		$reply_id=$_POST['reply_id']; //回复ID
		$comment=$_POST['comment'];	  //评论内容
		$user_type=$_POST['user_type'];	//回复类型

		if($user_id!=''){
			$array=array('topic_id'=>$id,'user_id'=>$user_id,'comment'=>$comment,'reply_id'=>$reply_id,'reply_type'=>$user_type,'create_time'=>time());
			//判断是否自己回复自己(暂时关闭)
			// if($reply_id==$user_id){
			// 	$array['reply_id']=0;
			// }
			$res=$commonc->insert($table,$array);
			if($res){
				$mes='评论成功';

				$user=$userc->userInfo($user_id);
				$userName=$user['account'];

				/*回复AJAX-start*/
				if($array['reply_id']>0){
					if($user_type=='user'){
						$reply=$userc->userInfo($reply_id);
						$replyName=$reply['account'];
						$reply="回复&nbsp;@<a href=user_main.php?type=user&id=".$reply_id.">".$replyName."</a>：";
					}else{
						$replyName='小编';
						$reply="回复&nbsp;@<a href=user_main.php?type=admin&id=".$reply_id.">".$replyName."</a>：";
					}	
				}	
 
		       $html='<span class="j_tctk_hf">
                 <a href="user_main.php?type=user&id='.$user_id.'">'.$userName.'</a>：'.$reply.'<span class="neirong">'.$comment.'</span> &nbsp;&nbsp;<a href="javascript:;" class="dan_hf" style="float:right"  name='.$userName.' reply_id='.$user_id.' user_type="user">回复 </a></span>';
			    /*回复AJAX-end*/
			}else{
				$mes='评论失败，请重试';
			}
			$error=1;
		}else{
			$error=0;
		}
		$arr=array('error'=>$error,'mes'=>$mes,'html'=>$html);
		echo json_encode($arr);
		exit();
	}
	/******评论end*******/

	//咨询分类
	$info_class=$infoc->infoClassList();
	$smarty->assign('info_class',$info_class['result']);

	//资讯列表
	$where='';
	$where.=" AND i.check='pass' ORDER BY i.create_time desc limit 0,10";
	$key='i.*';
	$row=$infoc->infoList($where,$key,0);

	//时间分组
	$now_time=date('Y-m-d');
	$smarty->assign('now_time',$now_time);	//当前时间
	$start=strtotime($now_time." 00:00:00");

	//分级
	for ($i=0; $i <count($row['result']) ; $i++) { 
		if($row['result'][$i]['create_time']>$start){
			//当天
			$row['result']['one'][]=$row['result'][$i];
		}elseif($row['result'][$i]['id']){
			//非当天
			$row['result']['two'][]=$row['result'][$i];
		}
	}

  	$smarty->assign('row',$row['result']);

  	//当天数量 
	$nowNum=count($row['result']['one']);
	$smarty->assign('nowNum',$nowNum);

	/*******小编说**********/
	//吐槽
	$where1=' AND a.info_id=0';
	$comment1=$infoc->topicList($where1);
	for ($i=0; $i <count($comment1['result']) ; $i++) { 
		$comment1['result'][$i]['hf_num']=0;
	}
	//计算总数
	$commentNum=count($comment1['result']);
	$smarty->assign('commentNum',$commentNum);
	//吐槽评论
	$where2='';
	$key="a.*,u.account,u.avatar,u1.account as account1";
	$comment2=$infoc->commentList($where2,$key);
	
	$comment=array();
	if(is_array($comment1['result'])){
		foreach ($comment1['result'] as $k1 => $v1) {
			$comment[$k1]=$v1;					//评论
			if(is_array($comment2['result'])){
				foreach ($comment2['result']as $k2=> $v2) {
					//处理评论跟回复
					if($v2['type']=='user'){
						$v2['userName']=$v2['account'];
					}else{
						$v2['userName']='小编';
					}
					//判断是否属于回复
					if($v2['reply_id']>0){
						if($v2['reply_type']=='user'){
							$v2['replyName']=$v2['account1'];
						}else{
							$v2['replyName']='小编';
						}			
					}
					//组装
					if($v1['id']==$v2['topic_id'] && $v1['hf_num']<5){
						$comment[$k1]['reply'][]=$v2;
						$v1['hf_num']=$v1['hf_num']+1;
					}	
				}
			}
		}
	}
	$smarty->assign('comment',$comment);


	$smarty->display("index.html");
?>